package com.hnkywl.sts.service.cggl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.cggl.CgdmxDao;
import com.hnkywl.sts.entity.cggl.Cgdmx;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-23
 * Time: 上午11:10
 * To change this template use File | Settings | File Templates.
 */
@Service
public class CgdmxService {
    private CgdmxDao cgdmxDao;

    //查询采购单明细
    public String findCgmxList(String cgdh, Long gysId, Long ckId, String code, String name, String ggxh, Long sbgsId, Long sbbmId, String zt, Date fhrqStart, Date fhrqEnd, int page, int rows) throws ParseException {
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        Page cgdmxPage = this.cgdmxDao.findAllCgdmxList(cgdh, gysId, ckId, code, name, ggxh, sbgsId, sbbmId, zt, fhrqStart, fhrqEnd, page, rows);
        List<Map> sumList = cgdmxDao.findSumCgdmx(cgdh, gysId, ckId, code, name, ggxh, sbgsId, sbbmId, zt, fhrqStart, fhrqEnd);
        String cgdmxJson = gson.toJson(cgdmxPage.getResult());
        Double cgsl = 0d;
        Double cgrksl = 0d;
        Double bhse = 0d;
        Double hse = 0d;
        if (sumList.size() != 0) {
            java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
            java.text.DecimalFormat jtdf = new java.text.DecimalFormat("0.00");
            for (Map map : sumList) {
                cgsl += Double.valueOf((map.get("sumsl") == null ? 0d : map.get("sumsl")).toString());
                cgrksl += Double.valueOf((map.get("sumrksl") == null ? 0d : map.get("sumrksl")).toString());
                hse += Double.valueOf((map.get("sumhse") == null ? 0d : map.get("sumhse")).toString());
                bhse += Double.valueOf((map.get("sumbhse") == null ? 0d : map.get("sumbhse")).toString());
            }
            return "{\"total\":" + cgdmxPage.getTotalCount() + ",\"rows\":" + cgdmxJson + ",\"footer\":[{\"cgdh\":\"合计:\",\"gys_jc\":\"采购数量:\",\"ck_mc\":" + df.format(cgsl) + ",\"gg_code\":\"入库数量:\",\"gg_name\":" + df.format(cgrksl) + ",\"gg_ggxh\":\"含税总金额(元):\",\"gg_dw\":" + jtdf.format(hse) + ",\"sbsl\":\"不含税总金额(元):\",\"rksl\":" + jtdf.format(bhse) + "}]}";
        } else {
            return "{\"total\":" + cgdmxPage.getTotalCount() + ",\"rows\":" + cgdmxJson + "}";
        }
    }

    public Cgdmx findCgdmxById(Long id) {
        return cgdmxDao.get(id);
    }

    @Autowired
    public void setCgdmxDao(CgdmxDao cgdmxDao) {
        this.cgdmxDao = cgdmxDao;
    }
}
